package com.ayf.payment.game.api.controller.platform;

import com.ayf.payment.game.api.dto.PageDTO;
import com.ayf.payment.game.api.dto.criteria.PageCriteria;
import com.ayf.payment.game.api.entity.platform.SystemBulletin;
import com.ayf.payment.game.api.service.platform.SystemBulletinService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: zh-liang
 * @Date : 2020-03-09 15:57
 */
@RestController
@RequestMapping("/t/bulletin")
public class SystemBulletinController {

    @Autowired
    private SystemBulletinService systemBulletinService;

    @PostMapping("/insert")
    public CommonResult insert(@RequestBody SystemBulletin systemBulletin) {
        if (systemBulletinService.insert(systemBulletin)) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "系统公告添加成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR, "系统公告添加失败");
    }

    @PostMapping("/delete")
    public CommonResult delete(@RequestBody SystemBulletin systemBulletin) {
        if (systemBulletinService.delete(systemBulletin.getId())) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "系统公告删除成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR, "系统公告删除失败");
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody SystemBulletin systemBulletin) {
        if (systemBulletinService.update(systemBulletin)) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "系统公告修改成功");
        }
        return new CommonResult(CommonConstants.RETURN_ERROR, "系统公告修改失败");
    }

    @PostMapping("/selectOne")
    public CommonResult<SystemBulletin> selectOne(@RequestBody SystemBulletin systemBulletin) {
        SystemBulletin bulletin = systemBulletinService.selectOne(systemBulletin.getId());
        return new CommonResult<SystemBulletin>(CommonConstants.RETURN_SUCCESS, "系统公告查询成功", bulletin);

    }

    @PostMapping("/list")
    public CommonResult<PageDTO> list(@RequestBody PageCriteria pageCriteria) {
        PageHelper.startPage(pageCriteria.getPageNum(), pageCriteria.getPageSize());
        List<SystemBulletin> list = systemBulletinService.list();
        PageInfo<SystemBulletin> pageInfo = new PageInfo<>(list);
        PageDTO dto = BeanConvertUtils.convert(pageInfo, PageDTO.class);
        return new CommonResult<PageDTO>(CommonConstants.RETURN_SUCCESS, "系统公告查询成功", dto);
    }

    @PostMapping("/selectBulletin")
    public CommonResult<SystemBulletin> selectBulletin(){
        List<SystemBulletin> list = systemBulletinService.list();
        if (list == null || list.size() == 0) {
            return new CommonResult<SystemBulletin>(CommonConstants.RETURN_SUCCESS, "系统公告查询成功", null);
        }
        return new CommonResult<SystemBulletin>(CommonConstants.RETURN_SUCCESS, "系统公告查询成功", list.get(0));
    }
    @PostMapping("/deleteAll")
    public CommonResult deleteAll(){
        systemBulletinService.deleteAll();
        return new CommonResult(CommonConstants.RETURN_SUCCESS, "系统公告清空成功");
    }
}
